/*** 
 * @_______________#########_______________________ 
 * @______________############_____________________ 
 * @______________#############____________________ 
 * @_____________##__###########___________________ 
 * @____________###__######_#####__________________ 
 * @____________###_#######___####_________________ 
 * @___________###__##########_####________________ 
 * @__________####__###########_####_______________ 
 * @________#####___###########__#####_____________ 
 * @_______######___###_########___#####___________ 
 * @_______#####___###___########___######_________ 
 * @______######___###__###########___######_______ 
 * @_____######___####_##############__######______ 
 * @____#######__#####################_#######_____ 
 * @____#######__##############################____ 
 * @___#######__######_#################_#######___ 
 * @___#######__######_######_#########___######___ 
 * @___#######____##__######___######_____######___ 
 * @___#######________######____#####_____#####____ 
 * @____######________#####_____#####_____####_____ 
 * @_____#####________####______#####_____###______ 
 * @______#####______;###________###______#________ 
 * @________##_______####________####______________ 
 * @
 * @Author: ipk518 18163976442@163.com
 * @Date: 2024-07-24 09:24:00
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-03-07 14:15:01
 * @FilePath: /etws-sxk2307/src/packetResolver/waveCmdV1/waveProcotol.cpp
 * @Description: 
 * @************佛祖保佑************
 * @Copyright (c) 2024 by etws@quyujiang, All Rights Reserved. 
 */

/*
 * @FilePath: /etws-sxk2307/src/packetResolver/waveCmdV1/waveProcotol.cpp
 * @Description:  
 * @Author: ipk518 && 18163976442@163.com
 * @Date: 2024-07-24 09:24:00
 * @LastEditors: Do not edit
 * @LastEditTime: 2024-07-25 14:08:51
 * Copyright    : G AUTOMOBILE RESEARCH INSTITUTE CO.,LTD Copyright (c) 2024.
 */

#include "Server.h"
#include "waveCmdV1.h"
#include "Uio.h"

/**
 * @brief: 发射
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
int wave_callback_0(struct cmdDataType* cmd,uint8_t * cmdData)
{
    if(cmd == nullptr && cmdData == nullptr) {
        qlog_e("发射读取指令无效..");
        return -1;
    }
    if(Server::Instance().m_sxk.m_sxkInfo.m_status.m_timingMode != 0x01) {
        qlog_e("系统未启动，无法观测..");
        return -1;
    }
    Server::Instance().m_sxk.m_uio->m_txWork.update = true;//获取一次观测数据
    return 0;
}

/*** 
 * @description: 接收观测
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
int wave_callback_1(struct cmdDataType* cmd,uint8_t * cmdData)
{
    if(cmd == nullptr && cmdData == nullptr) {
        qlog_e("接收读取指令无效..");
        return -1;
    }
    int m_freq = *cmdData;
    int m_polar = *(cmdData+1);
    uioMode tigger = uio_rx_obsChan0;
    if(*cmdData < 2) {
        Server::Instance().m_sxk.m_uio->m_rxWork.m_freqPoint = *cmdData;
    } else {
        qlog_e("wave设置接收频点无效..取值0-1");
        return -1;
    }

    if(*(cmdData+1) < 2) {
        Server::Instance().m_sxk.m_uio->m_rxWork.m_polar = *cmdData;
    } else {
        qlog_e("wave设置接收极化无效..取值0-1");
        return -1;
    }

    if(*(cmdData+2) < 32) {
        Server::Instance().m_sxk.m_uio->m_rxWork.m_channel = *(cmdData+1);
    } else {
        qlog_e("wave设置接收通道无效..取值0-31");
        return -1;
    }
    if(*(uint16_t *)(cmdData+3) < 4096) {
        Server::Instance().m_sxk.m_uio->m_rxWork.m_simple = *(uint16_t *)(cmdData+2);
    } else {
        qlog_e("wave设置接收采样点无效..取值1-4095");
        return -1;
        
    }
    qlog_i("接收观测,触发极化:%d,触发频点:%d.",m_polar,m_freq);
    if (m_polar == 0) {//H极化
        tigger = (m_freq == 0)?uio_rx_obsChan0:uio_rx_obsChan1;
    } else {
        tigger = (m_freq == 0)?uio_rx_obsChan2:uio_rx_obsChan3;
    }
    Server::Instance().m_sxk.m_uio->tiggerObsSem(tigger);
    return 0;

}

/*** 
 * @description: ADC性能测试
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
int wave_callback_2(struct cmdDataType* cmd,uint8_t * cmdData)
{
    if(cmd == nullptr && cmdData == nullptr) {
        qlog_e("接收读取指令无效..");
        return -1;
    }
    
    if(*cmdData < 4) {
        Server::Instance().m_sxk.m_uio->m_adcWork.m_channel = *(uint16_t *)cmdData;
    } else {
        qlog_e("wave设置接收通道无效..取值0-3");
        return -1;
    }
    if(*(uint16_t *)(cmdData+1) < 32768) {
        Server::Instance().m_sxk.m_uio->m_adcWork.m_simple = *(uint16_t *)(cmdData+1);
    } else {
        qlog_e("wave设置接收采样点无效..取值1-32768");
        return -1;
        
    }
    Server::Instance().m_sxk.m_uio->tiggerObsSem(uio_adc_observer);
    return 0;

}

/*** 
 * @description: DBF观测
 * @param {cmdDataType*} cmd
 * @param {uint8_t *} cmdData
 * @return {*}
 */
int wave_callback_3(struct cmdDataType* cmd,uint8_t * cmdData)
{
    if(cmd == nullptr && cmdData == nullptr) {
        qlog_e("DBF读取指令无效..");
        return -1;
    }
    
    if(*cmdData < 2) {
        Server::Instance().m_sxk.m_uio->m_dbfWork.m_polar = *cmdData;
    } else {
        qlog_e("wave设置DBF极化无效..取值0-1");
        return -1;
    }

    if(*(cmdData+1) < 16) {
        Server::Instance().m_sxk.m_uio->m_dbfWork.m_channel = *(cmdData+1);
    } else {
        qlog_e("wave设置DBF通道无效..取值0-15");
        return -1;
    }
    if(*(uint16_t *)(cmdData+2) < 4096) {
        Server::Instance().m_sxk.m_uio->m_dbfWork.m_simple = *(uint16_t *)(cmdData+2);
    } else {
        qlog_e("wave设置DBF采样点无效..取值1-4095");
        return -1;
        
    }
    Server::Instance().m_sxk.m_uio->tiggerObsSem(uio_dbf_observer);
    return 0;

}